package com.byapp.fast.common.converter;


import com.byapp.fast.common.enums.PlayIdEnum;
import com.byapp.fast.modules.contract.dto.BetItemsDTO;
import com.byapp.fast.modules.match.entity.MatchOddsEntity;
import com.byapp.fast.modules.match.entity.MatchRefEntity;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class BetItemsDTOConverter {

    public static List<BetItemsDTO> mapToTarget(MatchOddsEntity matchOddsEntity,
                                                MatchRefEntity matchRefEntity) {
        // 获取胜平负赔率
        String win = matchOddsEntity.getWin();
        String even = matchOddsEntity.getEven();
        String lost = matchOddsEntity.getLost();

        // 获取竞猜内容
        String homeTeam = matchRefEntity.getHomeTeamName();
        String awayTeam = matchRefEntity.getAwayTeamName();

        // 获取PlayId
        String playId = matchOddsEntity.getPlayId();

        String handicap = null;
        String awayHandicap = null;

        // 亚盘
        if (StringUtils.equals(PlayIdEnum.GLOBAL_ASIAN.getCode().toString(), playId)
                || StringUtils.equals(PlayIdEnum.HALF_ASIAN.getCode().toString(), playId)) {
            handicap = matchOddsEntity.getHandicap();
            awayHandicap = StringUtils.isEmpty(handicap) ? null : StringUtils.startsWith(handicap, "-") ?
                    StringUtils.substringAfter(handicap, "-") : "-" + handicap; // 客队盘口为主队盘口取反
        }


        Map<String, String> map = new HashMap<>();

        map.put("0", lost);
        map.put("1", even);
        map.put("3", win);

        if(even == null){
            map.remove("1");
        }

        // 对象转换
        String finalHandicap = handicap; // lambda 只能传入final类型字段，所以对盘口进行重新赋值，得到最终的盘口
        String finalAwayHandicap = awayHandicap;

        return map.entrySet().stream()
                .filter(entry -> StringUtils.isNotEmpty(entry.getValue()))
                .map(entry -> {
                            String key = entry.getKey();
                            BetItemsDTO betItemsDTO = new BetItemsDTO()
                                    .setBetOdds(entry.getValue())
                                    .setItemCode(key);
                            switch (key) {
                                case "0":
                                    betItemsDTO.setItemName(StringUtils.appendIfMissing(awayTeam, finalHandicap));
                                    break;
                                case "1":
                                    betItemsDTO.setItemName("平局");
                                    break;
                                case "3":
                                    betItemsDTO.setItemName(StringUtils.appendIfMissing(homeTeam, finalAwayHandicap));
                                    break;
                                default:
                                    break;
                            }
                            return betItemsDTO;
                        }

                ).collect(Collectors.toList());
    }
}
